﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;
using Intorise.Common;

namespace Model
{
    public class MainConfig : Config.AppConfig
    {
        /// <summary>
        /// 记录服务器信息
        /// </summary>
        public bool LogServerInfo { get; set; }
        /// <summary>
        /// 记录调试信息
        /// </summary>
        public bool LogDebugInfo { get; set; }
        /// <summary>
        /// 出现验证码是否断线重连
        /// </summary>
        public bool ValidateCodeReconnect { get; set; }

        /// <summary>
        /// 重启分钟数
        /// </summary>
        public int RebootMinute { get; set; }

        public MainConfig()
        {
            fileName = "AppConfig.config";
        }

        public override void Load()
        {
            base.Load();
            XmlSerializer serializer = new XmlSerializer(typeof(MainConfig));
            FileStream stream = null;
            if (!File.Exists(fileName))
            {
                this.LogServerInfo = false;
                this.ValidateCodeReconnect = false;
                this.LogDebugInfo = false;
                Save();
            }
            try
            {
                stream = File.Open(fileName, FileMode.Open);
                var config = (MainConfig)serializer.Deserialize(stream);
                this.LogServerInfo = config.LogServerInfo;
                this.ValidateCodeReconnect = config.ValidateCodeReconnect;
                this.LogDebugInfo = config.LogDebugInfo;
                this.RebootMinute = config.RebootMinute < 1 ? 240 : config.RebootMinute;
            }
            catch (Exception ex)
            {
                ConsoleBLL.WriteLineError("加载配置数据出错！\r\n" + ex.Message);
            }
            finally
            {
                if (stream != null) { stream.Close(); }
            }
        }
    }
}
